﻿@model AddressAttributeModel
@{
    if (!string.IsNullOrEmpty(Model.Id))
    {
        <div class="panel panel-default">
            <div class="panel-body">
                <vc:admin-widget widget-zone="address_attribute_details_values_top" additional-data="Model"/>
                <div id="addressattributevalues-grid"></div>
                <vc:admin-widget widget-zone="address_attribute_details_values_bottom" additional-data="Model"/>
            </div>
            <div class="panel-footer">
                <a id="btnAddNewValue" href="@(Url.Action("ValueCreatePopup", "AddressAttribute", new { addressAttributeId = Model.Id, area = Constants.AreaAdmin }))" class="k-button"><i class="fa fa-plus"></i>&nbsp;@Loc["Admin.Address.AddressAttributes.Values.AddNew"]</a>
                <input type="submit" id="btnRefresh" style="display: none"/>
            </div>
        </div>
        <script>
            $(document).ready(function () {
                $('#btnAddNewValue').magnificPopup({
                    type: 'ajax',
                    settings: { cache: false, async: false },
                    midClick: true,
                    callbacks: {
                        parseAjax: function (mfpResponse) {
                            mfpResponse.data = $('<div></div>').html(mfpResponse.data);
                        },
                        ajaxContentAdded: function () {
                            $('.mfp-wrap').removeAttr('tabindex');
                        }
                    }
                });
                $("#addressattributevalues-grid").kendoGrid({
                    dataBound: function () {
                        $('.editvalue').magnificPopup({
                            type: 'ajax',
                            settings: { cache: false, async: false },
                            midClick: true,
                            callbacks: {
                                parseAjax: function (mfpResponse) {
                                    mfpResponse.data = $('<div></div>').html(mfpResponse.data);
                                },
                                ajaxContentAdded: function () {
                                    $('.mfp-wrap').removeAttr('tabindex');
                                }
                            }
                        });
                    },
                    dataSource: {
                        transport: {
                            read: {
                                url: "@Html.Raw(Url.Action("ValueList", "AddressAttribute", new { addressAttributeId = Model.Id, area = Constants.AreaAdmin }))",
                                type: "POST",
                                dataType: "json",
                                data: addAntiForgeryToken
                            },
                            destroy: {
                                url: "@Html.Raw(Url.Action("ValueDelete", "AddressAttribute", new { area = Constants.AreaAdmin }))",
                                type: "POST",
                                dataType: "json",
                                data: addAntiForgeryToken
                            }
                        },
                        schema: {
                            data: "Data",
                            total: "Total",
                            errors: "Errors",
                            model: {
                                id: "Id"
                            }
                        },
                        error: function(e) {
                            display_kendoui_grid_error(e);
                            // Cancel the changes
                            this.cancelChanges();
                        },
                        serverPaging: true,
                        serverFiltering: true,
                        serverSorting: true
                    },
                    pageable: {
                        refresh: true,
                        numeric: false,
                        previousNext: false,
                        info: false
                    },
                    editable: {
                        confirmation: true,
                        mode: "inline"
                    },
                    scrollable: false,
                    columns: [{
                        field: "Name",
                        title: "@Loc["Admin.Address.AddressAttributes.Values.Fields.Name"]",
                        template: "<a class='k-link editvalue' href=\"@Url.Action("ValueEditPopup", "AddressAttribute", new { area = Constants.AreaAdmin })/#=Id#?addressAttributeId=@(Model.Id)\">#=Name#</a>",
                    }, {
                        field: "IsPreSelected",
                        title: "@Loc["Admin.Address.AddressAttributes.Values.Fields.IsPreSelected"]",
                        width: 100,
                        headerAttributes: { style: "text-align:center" },
                        attributes: { style: "text-align:center" },
                        template: '# if(IsPreSelected) {# <i class="fa fa-check" aria-hidden="true" style="color:green"></i> #} else {# <i class="fa fa-times" aria-hidden="true" style="color:red"></i> #} #'
                    }, {
                        field: "DisplayOrder",
                        title: "@Loc["Admin.Address.AddressAttributes.Values.Fields.DisplayOrder"]",
                        width: 100
                    }, {
                        command: {name: "destroy", text: "@Loc["Admin.Common.Delete"]"},
                        title: "@Loc["Admin.Common.Delete"]",
                        width: 100
                    }]
                });
            });
        </script>
        <script>
            $(document).ready(function () {
                $('#btnRefresh').click(function () {
                    //refresh grid
                    var grid = $("#addressattributevalues-grid").data('kendoGrid');
                    grid.dataSource.read();

                    //return false to don't reload a page
                    return false;
                });
            });
        </script>
    }
    else
    {
        <div class="note note-info">
            @Loc["Admin.Address.AddressAttributes.Values.SaveBeforeEdit"]
        </div>
    }
}